package com.yb.livewykt.ui.model

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.text.TextUtils
import com.yb.livewykt.bean.LightMakeupItem
import com.yb.livewykt.util.copyRgbaByteFromBitmap
import org.json.JSONException
import org.json.JSONObject
import java.io.IOException
import java.io.InputStream

class MakeupParamHelper {


    companion object{
        /**
         * 根据轻美妆类型，获取纹理贴图关键字
         *
         * @param type
         * @return
         */
        fun getMakeupTextureKeyByType(type: Int): String {
            return when (type) {
                LightMakeupItem.FACE_MAKEUP_TYPE_LIPSTICK -> "tex_lip"
                LightMakeupItem.FACE_MAKEUP_TYPE_EYE_LINER -> "tex_eyeLiner"
                LightMakeupItem.FACE_MAKEUP_TYPE_BLUSHER -> "tex_blusher"
                LightMakeupItem.FACE_MAKEUP_TYPE_EYE_PUPIL -> "tex_pupil"
                LightMakeupItem.FACE_MAKEUP_TYPE_EYEBROW -> "tex_brow"
                LightMakeupItem.FACE_MAKEUP_TYPE_EYE_SHADOW -> "tex_eye"
                LightMakeupItem.FACE_MAKEUP_TYPE_EYELASH -> "tex_eyeLash"
                else -> ""
            }
        }


        /**
         * 读取 RGBA 颜色数据
         *
         * @param context
         * @param colorAssetPath
         * @return
         */
        fun readRgbaColor(context: Context, colorAssetPath: String?): DoubleArray? {
            if (TextUtils.isEmpty(colorAssetPath)) {
                return null
            }
            try {
                context.assets.open(colorAssetPath!!).use { it ->
                    val bytes = ByteArray(it.available())
                    it.read(bytes)
                    val jsonObject = JSONObject(String(bytes))
                    val jsonArray = jsonObject.optJSONArray("rgba")
                    val colors = DoubleArray(jsonArray.length())
                    var i = 0
                    val length = jsonArray.length()
                    while (i < length) {
                        colors[i] = jsonArray.optDouble(i)
                        i++
                    }
                    return colors
                }
            } catch (e: IOException) {

            } catch (e: JSONException) {
            }
            return null
        }


        /**
         * 加载美妆贴图资源，返回图像的字节数组和宽高。
         *
         * @param context
         * @param resourcePath
         * @return TextureImage: width, height and bytes
         */
        fun createTextureImage(
            context: Context,
            resourcePath: String?
        ): TextureImage? {
            if (TextUtils.isEmpty(resourcePath)) {
                return null
            }
            var inputStream: InputStream? = null
            var bitmap: Bitmap?
            try {
                inputStream = context.assets.open(resourcePath!!)
                bitmap = BitmapFactory.decodeStream(inputStream)
            } catch (e: IOException) {
                // open assets failed, then try sdcard
                bitmap = BitmapFactory.decodeFile(resourcePath)
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close()
                    } catch (e: IOException) {
                        // ignored
                    }
                }
            }
            if (bitmap != null) {
                val width = bitmap.width
                val height = bitmap.height
                val bitmapBytes: ByteArray = copyRgbaByteFromBitmap(bitmap)
                return TextureImage(width, height, bitmapBytes)
            }
            return null
        }


    }



    /**
     * 根据轻美妆类型，获取妆容强度关键字
     *
     * @param type
     * @return
     */
    fun getMakeupIntensityKeyByType(type: Int): String? {
        return when (type) {
            LightMakeupItem.FACE_MAKEUP_TYPE_LIPSTICK -> "makeup_intensity_lip"
            LightMakeupItem.FACE_MAKEUP_TYPE_EYE_LINER -> "makeup_intensity_eyeLiner"
            LightMakeupItem.FACE_MAKEUP_TYPE_BLUSHER -> "makeup_intensity_blusher"
            LightMakeupItem.FACE_MAKEUP_TYPE_EYE_PUPIL -> "makeup_intensity_pupil"
            LightMakeupItem.FACE_MAKEUP_TYPE_EYEBROW -> "makeup_intensity_eyeBrow"
            LightMakeupItem.FACE_MAKEUP_TYPE_EYE_SHADOW -> "makeup_intensity_eye"
            LightMakeupItem.FACE_MAKEUP_TYPE_EYELASH -> "makeup_intensity_eyelash"
            else -> ""
        }
    }


    class TextureImage internal constructor(val width: Int, val height: Int, val bytes: ByteArray) {

        override fun toString(): String {
            return "TextureImage{" +
                    "width=" + width +
                    ", height=" + height +
                    ", bytes=" + bytes +
                    '}'
        }
    }

    object MakeupParam {
        /**
         * tex_ 开头的参数表示 fuCreateTexForItem 方法的 name 参数
         */
        const val TEX_BROW = "tex_brow"
        const val TEX_EYE = "tex_eye"
        const val TEX_PUPIL = "tex_pupil"
        const val TEX_EYE_LASH = "tex_eyeLash"
        const val TEX_EYE_LINER = "tex_eyeLiner"
        const val TEX_BLUSHER = "tex_blusher"
        const val TEX_FOUNDATION = "tex_foundation"
        const val TEX_HIGHLIGHT = "tex_highlight"
        const val TEX_SHADOW = "tex_shadow"

        /**
         * 是否使用双色口红，1 为开，0 为关
         */
        const val IS_TWO_COLOR = "is_two_color"

        /**
         * 口红类型，0 雾面，1 缎面，2 润泽，3 珠光
         */
        const val LIP_TYPE = "lip_type"

        /**
         * 嘴唇优化效果开关，1 为开，0 为关
         */
        const val MAKEUP_LIP_MASK = "makeup_lip_mask"

        /**
         * alpha 值逆向，1 为开，0 为关
         */
        const val REVERSE_ALPHA = "reverse_alpha"

        /**
         * 点位镜像，1 为开，0 为关
         */
        const val IS_FLIP_POINTS = "is_flip_points"

        /**
         * 是否使用眉毛变形，1 为开，0 为关
         */
        const val BROW_WARP = "brow_warp"

        /**
         * 眉毛变形类型
         */
        const val BROW_WARP_TYPE = "brow_warp_type"

        /**
         * 柳叶眉
         */
        const val BROW_WARP_TYPE_WILLOW = 0.0

        /**
         * 一字眉
         */
        const val BROW_WARP_TYPE_ONE_WORD = 1.0

        /**
         * 小山眉
         */
        const val BROW_WARP_TYPE_HILL = 2.0

        /**
         * 标准眉
         */
        const val BROW_WARP_TYPE_STANDARD = 3.0

        /**
         * 扶形眉
         */
        const val BROW_WARP_TYPE_SHAPE = 4.0

        /**
         * 日常风
         */
        const val BROW_WARP_TYPE_DAILY = 5.0

        /**
         * 日系风
         */
        const val BROW_WARP_TYPE_JAPAN = 6.0

        /**
         * 下面是各个妆容的颜色值
         */
        const val MAKEUP_EYE_BROW_COLOR = "makeup_eyeBrow_color"
        const val MAKEUP_LIP_COLOR = "makeup_lip_color"
        const val MAKEUP_LIP_COLOR2 = "makeup_lip_color2"
        const val MAKEUP_EYE_COLOR = "makeup_eye_color"
        const val MAKEUP_EYE_LINER_COLOR = "makeup_eyeLiner_color"
        const val MAKEUP_EYELASH_COLOR = "makeup_eyelash_color"
        const val MAKEUP_BLUSHER_COLOR = "makeup_blusher_color"
        const val MAKEUP_FOUNDATION_COLOR = "makeup_foundation_color"
        const val MAKEUP_HIGHLIGHT_COLOR = "makeup_highlight_color"
        const val MAKEUP_SHADOW_COLOR = "makeup_shadow_color"
        const val MAKEUP_PUPIL_COLOR = "makeup_pupil_color"

        /**
         * 在解绑妆容时，是否要清空妆容，0 表示不清除，1 表示清除
         */
        const val IS_CLEAR_MAKEUP = "is_clear_makeup"

        /**
         * 美妆开关，1 为开，0 为关
         */
        const val IS_MAKEUP_ON = "is_makeup_on"

        /**
         * 全局妆容强度，范围 [0-1]
         */
        const val MAKEUP_INTENSITY = "makeup_intensity"

        /**
         * 下面是各个妆容强度参数，范围 [0-1]
         */
        const val MAKEUP_INTENSITY_LIP = "makeup_intensity_lip"
        const val MAKEUP_INTENSITY_EYE_LINER = "makeup_intensity_eyeLiner"
        const val MAKEUP_INTENSITY_BLUSHER = "makeup_intensity_blusher"
        const val MAKEUP_INTENSITY_PUPIL = "makeup_intensity_pupil"
        const val MAKEUP_INTENSITY_EYE_BROW = "makeup_intensity_eyeBrow"
        const val MAKEUP_INTENSITY_EYE = "makeup_intensity_eye"
        const val MAKEUP_INTENSITY_EYELASH = "makeup_intensity_eyelash"
        const val MAKEUP_INTENSITY_FOUNDATION = "makeup_intensity_foundation"
        const val MAKEUP_INTENSITY_HIGHLIGHT = "makeup_intensity_highlight"
        const val MAKEUP_INTENSITY_SHADOW = "makeup_intensity_shadow"

        /*非参数值，App 内使用*/
        const val MAKEUP_INTENSITY_PREFIX = "makeup_intensity_"
        val MAKEUP_INTENSITIES = arrayOf(
            MAKEUP_INTENSITY_LIP,
            MAKEUP_INTENSITY_EYE_LINER,
            MAKEUP_INTENSITY_BLUSHER,
            MAKEUP_INTENSITY_PUPIL,
            MAKEUP_INTENSITY_EYE_BROW,
            MAKEUP_INTENSITY_EYE,
            MAKEUP_INTENSITY_EYELASH,
            MAKEUP_INTENSITY_FOUNDATION,
            MAKEUP_INTENSITY_HIGHLIGHT,
            MAKEUP_INTENSITY_SHADOW
        )
    }
}